package com.fuqi.android.zhongcai.business.shuig.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.fuqi.android.zhongcai.R;
import com.fuqi.android.zhongcai.base.view.activity.BaseActivity;
import com.fuqi.android.zhongcai.base.view.activity.ChooseWorkerActivity;
import com.fuqi.android.zhongcai.base.view.widget.wheelwidget.TimePickerView;
import com.fuqi.android.zhongcai.business.shuig.OrderDict;
import com.fuqi.android.zhongcai.business.shuig.request.api.OrderHandleApi;
import com.fuqi.android.zhongcai.business.shuig.request.result.OrderHandleResult;
import com.fuqi.android.zhongcai.business.shuig.view.widget.CJRow.ARResponse;
import com.fuqi.android.zhongcai.sql.Worker;
import com.fuqi.android.zhongcai.utils.ActivityUtils;
import com.fuqi.android.zhongcai.utils.DateUtil;
import com.fuqi.android.zhongcai.utils.RegexUtil;
import com.fuqi.framework.network.code.HandleCode;
import com.fuqi.framework.network.http.RequestManager;
import com.fuqi.framework.utils.StringUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

// 派工
public class ShuiGPaiGongActivity extends BaseActivity   {
    ARResponse response;
    List<Worker> workers ;

    Worker chargehand ;
    String sWorkerIds;
    String sWorkerNames;
    String sTime,sDate;


    @Bind(R.id.tv_worker)
    TextView mTvNames;
    @Bind(R.id.tv_date)
    TextView mTvDate;
    @Bind(R.id.tv_time)
    TextView mTvTime;
    @Bind(R.id.et_mi_shu)
    EditText mEtMeter;
    @Bind(R.id.et_remark)
    EditText mEtRemark;

    TimePickerView pvTime;

    @OnClick({R.id.rl_add_gr,R.id.iv_add_gr,R.id.btn_submit,R.id.tv_time,R.id.tv_date})
    public void onClick(View v){


        switch (v.getId()){
            case R.id.tv_date:
                pvTime = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
                pvTime.setTime(new Date());
                pvTime.setCyclic(false);
                pvTime.setCancelable(true);
                //时间选择后回调
                pvTime.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

                    @Override
                    public void onTimeSelect(Date date) {
                        sDate = DateUtil.getStringByFormat(date,DateUtil.dateFormatYMD);
                        mTvDate.setText(sDate);
                    }
                });
                pvTime.show();
                break;
            case R.id.tv_time:
                pvTime = new TimePickerView(this, TimePickerView.Type.HOURS_MINS);
                pvTime.setTime(new Date());
                pvTime.setCyclic(false);
                pvTime.setCancelable(true);
                //时间选择后回调
                pvTime.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

                    @Override
                    public void onTimeSelect(Date date) {
                        sTime = DateUtil.getStringByFormat(date,DateUtil.dateFormatHM);
                        mTvTime.setText(sTime);
                    }
                });
                pvTime.show();
                break;
            case R.id.rl_add_gr:
            case R.id.iv_add_gr:
                Intent intent = new Intent(ShuiGPaiGongActivity.this ,ChooseWorkerActivity.class);
                startActivityForResult(intent,10);
                break;
            case R.id.btn_submit:
                if(response != null) {
                    if(!RegexUtil.checkNotNull(mEtMeter.getText().toString())){
                        ActivityUtils.toast("请输入米数。");
                        break;
                    }else if(!RegexUtil.checkNotNull(sTime)){
                        ActivityUtils.toast("请选择时间。");
                        break;
                    }else if(!RegexUtil.checkNotNull(sDate)){
                        ActivityUtils.toast("请选择日期。");
                        break;
                    }else if(!RegexUtil.checkDate(sDate,sTime)){
                        ActivityUtils.toast("安装时间必须大于当前时间");
                        break;
                    }else if(!RegexUtil.checkNotNull(mTvNames.getText().toString())){
                        ActivityUtils.toast("请选择工人。");
                        break;
                    }

                    response.update("CHARGEHAND_ID", chargehand.getId());
                    response.update("CHARGEHAND_NAME", chargehand.getName());
                    response.update("CHARGEHAND_PHONE", chargehand.getId());
                    response.update("WORKER_IDS", sWorkerIds);
                    response.update("WORKER_NAMES", sWorkerNames);
                    response.update("APPOINTMENT_DATE", sDate);
                    response.update("APPOINTMENT_TIME", sTime);
                    response.update("CAPTAIN_REMARK", mEtRemark.getText().toString());
                    response.update("USE_METER", mEtMeter.getText().toString());
                    response.update("ORDER_STATUS", OrderDict.StatusType.DDZT_DCF);
                    RequestManager.getInstance().call(new OrderHandleApi(
                            new OrderHandleApi.OrderHandleParams(response.getDataWithMap()), respOrderHandleListener, errorListener));
                    Log.i("response : ", response.toString());
                }
                break;

        }


    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initView() {

        titleBar.setTitle("立即派工");

    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
        response = new ARResponse(getIntent().getStringExtra("response"));
    }


    Response.Listener<OrderHandleResult> respOrderHandleListener = new Response.Listener<OrderHandleResult>() {

        @Override
        public void onResponse(OrderHandleResult response) {

    if (HandleCode.requestSuccess()) {
                ActivityUtils.toast("完成派工。");
                finish();
                Log.i("juner", "juner respListener response : ");
            } else {



            }
        }
    };


    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {

            Log.i("juner", "juner  error.getMessage(): " + error.getMessage());

        }
    };

    @Override
    public boolean hasTitleBar() {
        return true;
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_pai_dan;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode == RESULT_OK){
            workers = (ArrayList<Worker>)data.getSerializableExtra("workers");
            sWorkerIds = "";
            sWorkerNames = "";
            for (int i = 0; i < workers.size(); i++) {
                if(i == 0){
                    chargehand = workers.get(i);
                    continue;
                }
                if(i != 1) {
                    sWorkerIds += ",";
                    sWorkerNames += ",";
                }
                sWorkerIds += workers.get(i).getId();
                sWorkerNames += workers.get(i).getName();
            }
            String names = (chargehand == null?"":chargehand.getName()) + (StringUtil.isEmpty(sWorkerNames) ? "" : ","+ sWorkerNames);
            mTvNames.setText(names);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
